/**
 * 
 */

window.onload = function(){
	
	xmlRequest();
	
//1、无条件查询用户信息
	//可改动选项：
	var tableName = "user";
	
	//获取元素
	var find = document.getElementById("find");
	
	//ajax请求
	function xmlRequest(){
		var xhr = new XMLHttpRequest();
		
		//4、改表头信息，与列名
		var tableHead = "<table>" +
					"<caption>用户管理</caption>" +
					"<tr>" +
						"<th>选项</th>" +
						"<th>用户ID</th>" +
						"<th>手机号</th>" +
						"<th>等级</th>" +
						"<th>操作</th>"+
					"</tr>";
		var tableBody="";
		var tableTail="</table>";
		 
		
		xhr.onreadystatechange = function(){
			if(xhr.readyState==4){
				if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
					
					var resT = xhr.responseText;
					if(resT!="0"){
						var userArray = JSON.parse(resT);
						//console.log("userArray:"userArray);
						
						for(var i=0;i<userArray.length;i++){
							if(i%2==0){
								tableBody+="<tr id='lightColor'>" +
								"<td><input type='checkbox' name='check' value='"+userArray[i].uId+"'/></td>" +
								"<td>"+userArray[i].uId+"</td>" +
								"<td>"+userArray[i].uPhone+"</td>" +
								"<td>"+userArray[i].uLevel+"</td>" +
								"<td><a href='javascript:link(\"../user_DeleteServlet?Ids=K "+userArray[i].uId+"\")'>删除</a></td>" +
								"</tr>";
							}else{
								tableBody+="<tr>" +
								"<td><input type='checkbox' name='check' value='"+userArray[i].uId+"'/></td>" +
								"<td>"+userArray[i].uId+"</td>" +
								"<td>"+userArray[i].uPhone+"</td>" +
								"<td>"+userArray[i].uLevel+"</td>" +
								"<td><a href='javascript:link(\"../user_DeleteServlet?Ids=K "+userArray[i].uId+"\")'>删除</a></td>" +
								"</tr>";
							}
						}
						find.innerHTML = tableHead+tableBody+tableTail;
					}else{
						find.innerHTML = "<p>表信息空空如也--''</p>";
					}
				}else{
					find.innerHTML="<p>抱歉！查询信息失败！</p>";
					console.log("查询失败！");
				}
			}
		}
		xhr.open("post","../user_findServlet",true);
		xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
		xhr.send("find=allInfo");
	}
	
	
//2、通过Uid 删除用户信息	
	//BEGIN  ———*————*———*————*删除功能———*————*———*————*———*————*
	
	var deleteItem = document.getElementById("delete");
	deleteItem.onclick = function(){
		deleteFunction();
	}
	
	//获取所选项Id
	function getCheckedId(){
		var checks = document.getElementsByName("check");
		var Ids="";
		for(var i=0;i<checks.length;i++){
			if(checks[i].checked){
				Ids+=checks[i].value+" ";
			}
		}
		console.log(Ids);
		return Ids;
	}
	
	//删除请求
	function deleteFunction(){
		var Ids = getCheckedId();
		//7、改动删除提示语：删除确定
		if(Ids!=""){
			var dlt = confirm("您确定要删除选中用户吗？");
			if(dlt){
				if(Ids!=""){
					//删除请求发送
					console.log("Ids："+Ids);
					var reAjax = deleteAjax(Ids);
					//删除结果提示
					if(reAjax!=0){
						alert("删除成功！");
						//隐藏删除功能区
					}else{
						alert("删除失败！");	
					}
					xmlRequest();
				}else{
					alert("请先选择您要删除的用户。");
				}
			}
		}
		else{
			alert("请先选择您要删除的用户。");
		}
	}
		
		
		//xhr发送获取到的id
		function deleteAjax(Ids){
			var xhr = new XMLHttpRequest();
			xhr.onreadystatechange = function(){
				if(xhr.state==4){
					if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
						var reAjax = xhr.response;
						if(reAjax!="0"){
							alert("删除成功！");
						}else{
							alert("删除失败！");
						}
					}else{
						console.log("删除失败！");
					}
				}
			}
			xhr.open("post","../user_DeleteServlet",true);
			xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
			xhr.send("Ids="+Ids);
		}
	
	
//事件区
	//全选/全反选事件
		var checkAll = document.getElementById("checkAll");
		checkAll.onclick = function(){
			var checks = document.getElementsByName("check");
			for(var i=0;i<checks.length;i++){
				checks[i].checked=!checks[i].checked;
			}
			checkAll.checked=!deleteBatch.checked;
		}
		
	//隐藏按钮事件
		
		var hiddenButtons = document.getElementById("hiddenButtons");
		
		var deleteBatch = document.getElementById("deleteBatch");
		deleteBatch.onclick = function(){
			hiddenButtons.style.visibility="visible";
		}
		
		
		var cancel = document.getElementById("cancel");
		cancel.onclick = function(){
			hiddenButtons.style.visibility="hidden";
		}	
	
	
	
}